﻿///<reference path="../../../Scripts/jquery-1.4.1-vsdoc.js"/>
///<reference path="../utils/Utils.js"/>
///<reference path="../controls/TreeViewBase.js"/>

function BiblePlayer(options) {

    ///public properties/////////////
    this.Testaments = [];  //Заветы
    
    ///events////////////////////////
    this.OnInitCompleted;


    ///private properties////////////

    // значения по умолчанию
    this.settings = {
        bibleFolder: "../data/bible",
        bibleMapXmlFileName: "map.xml"        
    };
    this.settings = $.extend(this.settings, options);  // заменяем значения переданными

    this.Init = function (onInitCompleted) {
        this.OnInitCompleted = onInitCompleted;
        appPointer.ServerManager.GetCoreItem(this.settings.bibleFolder + "/" + this.settings.bibleMapXmlFileName, true,
            ParseMapXmlFile);
    }

    this.GetBook = function(bookKey){
        for (var i = 0; i < this.Testaments.length; i++)
        {
            var book = this.Testaments[i].Books.Lookup(bookKey);
            if (book != undefined)
                return book;
        }

        return undefined;
    }

    this.Search = function (searchPattern) {
        var results = [];
        for (var i = 0; i < this.Testaments.length; i++) {
            var testament = this.Testaments[i];
            AddRange(results, testament.Search(searchPattern));
        }
        return results;
    }

    this.AddTestament = function (title) {
        return this.Testaments[this.Testaments.push(new BibleTestament(title)) - 1];
    }

    this.ParseBookFile = function (bookKey, xml) {
        var book = this.GetBook(bookKey);
        book.Xml = xml;
        var bookIndex = appPointer.KeyManager.GetBookIndex(bookKey);
        $(xml).find("chapter").each(function (chapterIndex) {
            var chapter = book.GetChapterByIndex(chapterIndex);

            chapter.Xml = this;

            $(chapter.Xml).find("verse").each(function (verseIndex) {
                var verse = chapter.AddVerse(appPointer.KeyManager.GetVerseKey(bookIndex, chapterIndex, verseIndex),
                    verseIndex + 1, this);               
            });
        });
    }

    function ParseMapXmlFile(xml) {
        xml = ClarifyXmlDom(xml);
        $("structure folder", xml).each(function (testamentIndex) {
            var testamentXml = $(this);
            var testament = appPointer.BiblePlayer.AddTestament(testamentXml.attr("title"));

            testamentXml.find("item").each(function () {
                var bookXml = $(this);

                var title = bookXml.attr("title");
                var bookIndex = bookXml.attr("index");
                var fullHref = appPointer.BiblePlayer.settings.bibleFolder + "/" + bookXml.attr("href");
                var key = appPointer.KeyManager.GetBookKey(bookIndex);
                var chaptersCount = bookXml.attr("chaptersCount");

                var book = testament.AddBook(key, title, chaptersCount);

                appPointer.CacheManager.RegisterCacheItem(key, 1,
                function () { return appPointer.ServerManager.GetCoreItem(fullHref); },
                function () {
                    appPointer.Logger.ShowShortMessage(this.Key + " downloaded");
                    appPointer.BiblePlayer.ParseBookFile(this.Key, this.Data);
                });

                for (var chapterIndex = 0; chapterIndex < chaptersCount; chapterIndex++) {
                    book.AddChapter(appPointer.KeyManager.GetChapterKey(bookIndex, chapterIndex), chapterIndex + 1);
                }
            });
        });
        
        if (appPointer.BiblePlayer.OnInitCompleted != undefined)
            appPointer.BiblePlayer.OnInitCompleted.call();
    }    
}